# -*- coding: utf-8 -*-
"""
@Time ： 2024/3/18 15:31
@Auth ： gc
"""
import math

import akshare as ak
import pandas as pd
import matplotlib.pyplot as plt


plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

l = []
c = None
codes = pd.read_csv("data/hs300_stocks.csv", converters = {"code":str})["code"].values.tolist()
for i, code in enumerate(codes):
    stock_individual_info_em_df = ak.stock_individual_info_em(symbol = code)
    if i == 0:
        c = stock_individual_info_em_df["item"].values.tolist()
    l.append(stock_individual_info_em_df["value"].values.tolist())
stock_zh_a_spot_em_df = ak.stock_zh_a_spot_em()
# stock_zh_a_spot_em_df[stock_zh_a_spot_em_df["代码"].isin(codes)].to_excel("./data/hs300_stocks_info_2.xlsx", index=False)
df = pd.merge(pd.DataFrame(l, columns = c)[["行业", "上市时间", "股票代码", "股票简称", "总股本", "流通股"]],
              stock_zh_a_spot_em_df[stock_zh_a_spot_em_df["代码"].isin(codes)], left_on = '股票代码', right_on = '代码',
              how = 'left')
hys = df["行业"].unique().tolist()
z, y = divmod(len(hys), 5)
if y>0:
    z+=1
fig, axes = plt.subplots(z, 5, figsize=(20, 20))
for i in range(z):
    for j in range(5):
        if i * 5 + j < len(hys):
            k = df[df["行业"] == hys[i * 5 + j]]
            axes[i][j].scatter(k["市盈率-动态"], k["市净率"])
            axes[i][j].set_title(hys[i * 5 + j])
            axes[i][j].set_xlabel("市盈率")
            axes[i][j].set_ylabel("市净率")
plt.show()

